package com.kyzh.core.helpers

import android.text.SpannableString
import android.text.Spanned
import android.text.style.RelativeSizeSpan
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.kyzh.core.R
import org.jetbrains.anko.backgroundResource

object DataBindingImageHelper {

    @BindingAdapter("imageUrl")
    @JvmStatic
    fun loadImage(imageView: ImageView, url: String?) {
        if(url!=null){
            if(!url.contains("http")){
                val url1 = "http:$url"
                Glide.with(imageView.context).load(url1).into(imageView)
            }else{
                Glide.with(imageView.context).load(url).into(imageView)
            }
        }

    }
    @BindingAdapter("loadImageCircle")
    @JvmStatic
    public fun loadImageCircle(imageView: ImageView, url: String?) {
        Glide.with(imageView.context).load(url)
            .placeholder(R.drawable.placeholder)
            .apply( RequestOptions()
                .transforms( CenterCrop(),  RoundedCorners(20)
                ))
            .error(R.drawable.placeholder)
            .into(imageView)
    }
    //设置文字可以缩放的
    @BindingAdapter("textSpan","tagtext")
    @JvmStatic
    public fun textSpan(textView: TextView, text: String?, tagtext:String) {
        var text1  = "$tagtext$text"
        val span = SpannableString(text1)
        span.setSpan(RelativeSizeSpan(1.7f), tagtext.length,text1.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        textView.text = span
    }
    //设置改变北京颜色的
    @BindingAdapter("checkBackGround")
    @JvmStatic
    public fun bgCheck(view: View, check: Boolean?) {
        check?.let {
            if(!it){
                view.backgroundResource = R.drawable.item_back_recover_nocheck
            }else{
                view.backgroundResource = R.drawable.item_back_recover
            }
        }
    }
}